<?php
ob_start();
session_start();
$sess_id=$_SESSION['sess_id'];
$sess_user=$_SESSION['sess_user'];
$sess_status=$_SESSION['sess_status'];
$flag = $_SESSION['sess_flag'];
//นำเข้าการตั้งค่าต่างๆและการติดต่อฐานข้อมูล
include('config.inc.php');
// ฟังก์ชั่นต่างเป็นฟังก์ชั้น php
include('runid.php');
// สีพื้นหลังของตารางที่ใช้ค้นหา
$color = '#996600';
// ตัวแปรที่รับเข้ามา
$typeSearch = $_POST['typesearch'];
// ตรวจสอบประเภทเพื่อจัดการกับ sql
$wordSearch = $_POST['wordsearch'];
// จำนวนแถวต่อการแสดงในหนึ่งหน้า
$per_page = $_POST['per_page'];
$strpage = $_POST['strpage'];
//echo "คำค้นที่ส่งมาคือ ".$wordSearch."<br>";
//echo "ประเภทการค้นหาที่ส่งมาคือ ".$typeSearch."<br>";
$sql = "SELECT * 
		FROM detail de
		left join approve ap  on de.no = ap.fault_no 
		left join fault_type ft on ft.id = de.fault_type 
		left join location lo on lo.location_id = de.location_id 
		left join status_type st on st.status_id = de.status ";
if($typeSearch == 1 && $wordSearch <> ""){
	$str = explode("/",$wordSearch);
	$wordSearch = $str[2]."-".$str[0]."-".$str[1];
	$sql .= "where de.day_in LIKE '%$wordSearch%' ";
	$bg1 = $color;
}else if($typeSearch == 2 && $wordSearch <> ""){
	$sql .= "where de.username_note LIKE '%$wordSearch%' ";
	$bg2 = $color;
}else if($typeSearch == 3 && $wordSearch <> ""){
	$sql .= "where de.problem LIKE '%$wordSearch%' ";
	$bg3 = $color;
}else if($typeSearch == 4 && $wordSearch <> ""){
	$text = explode("-",$wordSearch);
	$str1 = explode("/",$text[0]);
	$str2 = explode("/",$text[1]);
	$wordSearch1 = $str1[2]."-".$str1[0]."-".($str1[1]-1);
	$wordSearch2 = $str2[2]."-".$str2[0]."-".($str2[1]+1);
	$sql .= "where de.day_in BETWEEN '$wordSearch1' AND '$wordSearch2' ";
	$bg1 = $color;
}else if($typeSearch == 5 && $wordSearch <> ""){
	$sql .= "where lo.site_name LIKE '%$wordSearch%' OR lo.location_id LIKE '%$wordSearch%'";
	$bg3 = $color;
}else if($typeSearch == 6 && $wordSearch <> ""){
	$sql .= "where de.status LIKE '%$wordSearch%' OR ap.app_status LIKE '%$wordSearch%' ";
	$bg3 = $color;
}

@$result = mysql_query($sql);
@$num = mysql_num_rows($result);
echo "<div align='right' style='padding:10px 0px 10px 0px'><small>จำนวนรายการที่พบ &nbsp;<b>".$num."</b> รายการ</small></div>";
$page = $strpage;
if(!$strpage){ 
	$page =1;
}
$prev_page = $page-1;
$next_page = $page+1;
$page_start =  (($per_page*$page)-$per_page);
if($num <= $per_page){
	$num_page = 1;
}else if(($num % $per_page) == 0){
	$num_page = ($num/$per_page);
}else{
	$num_page = ($num/$per_page)+1;
	$num_page = (int)$num_page;
}
$sql .= " ORDER BY de.day_in ASC limit $page_start, $per_page";
$result = mysql_query($sql);
?>
<table width='100%' border='1' cellspacing='0' cellpadding='3'>
  <tr bgcolor="#535353">
    <td width="1%"><font color="#FFFFFF"><strong><small>ลำดับ</small></strong></font></td>
    <td width="15%"><font color="#FFFFFF"><small><strong>วันที่ทำการแจ้ง</strong></small></font></td>
    <td width="17%"><font color="#FFFFFF"><strong><small>หมายเลขการแจ้ง</small></strong></font></td>
    <td width="12%"><font color="#FFFFFF"><strong><small>ประเภทเหตุเสีย</small></strong></font></td>
    <td width="14%"><font color="#FFFFFF"><strong><small>สถานที่</small></strong></font></td>
    <td width="20%"><font color="#FFFFFF"><strong><small>เหตุเสีย</small></strong></font></td>
    <td width="13%"><font color="#FFFFFF"><strong><small>สถานะ</small></strong></font></td>
    <td width="8%"><font color="#FFFFFF"><strong><small>ผู้แจ้งเหตุเสีย</small></strong></font></td>
  </tr>
<?php
if($num != 0){
$n = 1;
while($arr=mysql_fetch_array($result)){
		if($n %2==0){
			$bgcolor = '#EBEBEB';
		}else{
			$bgcolor = '#FFFFFF';
		}
		echo "<tr bgcolor='$bgcolor'>";
		echo "<td align='center'><small>$n</small></td>";
		echo "<td ><font color='$bg1'><small>".thDate($arr[day_in])."&nbsp;</small></font></td>";
		if($flag == 2 ){
			if($arr[approve_no] == ""){
				echo "<td ><small><a href='showfaultdetail.php?no=".$arr[no]."&process=".$sess_user."' style='text-decoration:underline;color:#000000;' target='_blank'>".$arr[no]."</a>&nbsp;</small></td>";
			}else{
				echo "<td ><small><a href='showappdetail.php?app_no=".$arr[approve_no]."' style='text-decoration:underline;color:#000000;' target='_blank'>".$arr[no]."</a>&nbsp;</small></td>";
			}
		}else if($flag == 3){
			if($arr[approve_no] == ""){
				echo "<td ><small><a href='showfaultdetail.php?no=".$arr[no]."' style='text-decoration:underline;color:#000000;' target='_blank'>".$arr[no]."</a>&nbsp;</small></td>";
			}else if($flag == 3 ||($typeSearch == 6 && $wordSearch == 3)){
				echo "<td ><small><a href='showappdetail.php?app_no=".$arr[approve_no]."&app_u=".$sess_user."' style='text-decoration:underline;color:#000000;' target='_blank'>".$arr[no]."</a>&nbsp;</small></td>";
			}
		}else{
			echo "<td ><small><a href='showfaultdetail.php?no=".$arr[no]."' style='text-decoration:underline;color:#000000;' target='_blank'>".$arr[no]."&nbsp;</small></td>";
		}

			
		echo "<td ><small>".$arr[fault_name]."&nbsp;</small></td>";
		echo "<td ><small>".$arr[location_id]." : ".$arr[site_name]."&nbsp;</small></td>";
		echo "<td ><font color='$bg3'><small>".$arr[problem]."&nbsp;</small></font></td>";
		echo "<td ><small>".$arr[status_detail]."&nbsp;</small></td>";
		echo "<td ><font color='$bg2'><small>".$arr[username_note]."&nbsp;</small></font></td>";
		echo "</tr>";		
		$n++;
	}
}else{
	echo "<td colspan=8 align='center' style='padding:20px 0px 10px 0px;'><small><h2><font color='red'>ไม่พบรายการใดตรงกับคำค้นหาของคุณ</font></h2></small></td>";
}
//mysql_close($connect);
?>
</table>
<div align='right'>
<small>Total <?= $num;?> Record : <?=$num_page;?> Page :
<?
if($typeSearch == 1 && $wordSearch <> ""){
	$doc = 'document.fsearch.datefault.value,1';
}else if($typeSearch == 2 && $wordSearch <> ""){
	$doc = 'document.fsearch.wordsearch.value,2';
}else if($typeSearch == 3 && $wordSearch <> ""){
	$doc = 'document.fsearch.wordsearch.value,3';
}else if($typeSearch == 4 && $wordSearch <> ""){
	$doc = 'document.fsearch.datestart.value+\'-\'+document.fsearch.datestop.value,4';
}else if($typeSearch == 5 && $wordSearch <> ""){
	$doc = 'document.fsearch.locationcode.value,5';
}else if($typeSearch == 6 && $wordSearch <> ""){
	$doc = 'document.fsearch.statusdb.value,6,';
}

if($prev_page) 
{
	echo " <a href=\"JavaScript:getresult($doc,'$prev_page',document.fsearch.select_perpage.value)\" style='text-decoration:none'><< Back</a> ";
}

for($i=1; $i<=$num_page; $i++){
	if($i != $page)
	{
		echo "[ <a href=\"JavaScript:getresult($doc,'$i',document.fsearch.select_perpage.value)\" style='text-decoration:none'>$i</a> ]";
	}
	else
	{
		echo "<b> $i </b>";
	}
}
if($page!=$num_page)
{
	echo " <a href=\"JavaScript:javascript:getresult($doc,'$next_page',document.fsearch.select_perpage.value);\" style='text-decoration:none'>Next >></a> ";
}

mysql_close($connect);
?>
</small></div>